#include "msg_queue.h"
#include "stdio.h"

struct msg_queue queue;

void *produce(void * arg)
{
    pthread_detach(pthread_self());
    int i = 0;
    while(1){
		struct msg_buffer *strmsg = (struct msg_buffer*)malloc(sizeof(struct msg_buffer));
		strmsg->size = 10;
		char *a = (char *)malloc(10);
		sprintf(a, "produce%d", i++);
		strmsg->buffer = a;
        put_queue(&queue, strmsg);
		printf("produce strmsg Data:%s\n", a);
		usleep(100000);
		if(i == 15)
			break;
    }
	return NULL;
}

void *consume(void *arg)
{
    char *data;
    while(1){
		struct msg_buffer *strmsg = get_queue(&queue);
        data = (char *)(strmsg->buffer);
		printf("consume strmsg Data:%s\n", data);
		free(data);
		usleep(400000);
    }
}

int main()
{   
    pthread_t pid;
	
	init_queue(&queue, "test", 10);

    //pthread_create(&pid, 0, produce, 0);
    //pthread_create(&pid, 0, produce, 0);
    pthread_create(&pid, 0, produce, 0);
    pthread_create(&pid, 0, consume, 0);
    //pthread_create(&pid, 0, consume, 0);
    //pthread_create(&pid, 0, consume, 0);

    sleep(3);

    destroy_queue(&queue);
}